﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library;
using FRL.DebtChased.Silverlight.Library.RichContentService;

namespace FRL.DebtChased.WorkflowEditor
{

	/// <summary>
	/// Declaration of EditInvoiceStatusWnd class
	/// Window for editing an existing invoice status
	/// </summary>
	public partial class EditInvoiceStatusWnd : ChildWindow
	{

		/////////////////////////////////////////////////////////////
		// Attributes

		private int m_iAccountInvoiceStatusId = 0;

		/////////////////////////////////////////////////////////////
		// Construction

		public EditInvoiceStatusWnd()
		{

			// Load xaml resources
			InitializeComponent();

		}

		public EditInvoiceStatusWnd( AccountInvoiceStatusTransport status )
		{

			// Initialize member variables
			m_iAccountInvoiceStatusId = status.AccountInvoiceStatusId;

			// Load xaml resources
			InitializeComponent();

			// Set controls
			txtStatusName.Text = status.InvoiceStatusName;
			chkInstalmentPlan.IsChecked = status.IsInstalmentPlanStatus;
			chkInQuery.IsChecked = status.IsInQuery;

		}

		/////////////////////////////////////////////////////////////
		// Event Handlers

		private void OKButton_Click(object sender, RoutedEventArgs e)
		{

			// Validate controls
			List<string> strErrors = new List<string>();
			if (txtStatusName.Text.Length < 2)
				strErrors.Add("Invoice status name must be 2 characters.");
			if ((chkInstalmentPlan.IsChecked ?? false) && (chkInQuery.IsChecked ?? false))
				strErrors.Add("Invoice status cannot have be In-Query as well as an instalment plan.");
			if (strErrors.Count > 0)
			{
				MessageBoxes.PopupErrors(strErrors);
				return;
			}

			// Call server function
			RichContentServiceClient proxy = new RichContentServiceClient();
			proxy.Endpoint.Address = ExtensionsForSilverlight.CreateDebtChasedEndpoint();
			proxy.SetInvoiceStatusCompleted += new EventHandler<SetInvoiceStatusCompletedEventArgs>(proxy_SetInvoiceStatusCompleted);
			proxy.SetInvoiceStatusAsync(
				m_iAccountInvoiceStatusId,
				txtStatusName.Text,
				chkInstalmentPlan.IsChecked ?? false,
				chkInQuery.IsChecked ?? false
			);

		}

		void proxy_SetInvoiceStatusCompleted(object sender, SetInvoiceStatusCompletedEventArgs e)
		{

			// Get the result
			if (e.Error != null)
				MessageBoxes.PopupMessage(e.Error.Message);
			else if (e.Result != null)
				MessageBoxes.PopupMessage_InternalError(e.Result);
			else
			{

				// Close dialog
				this.DialogResult = true;

			}

		}

		private void CancelButton_Click(object sender, RoutedEventArgs e)
		{

			// Close dialog
			this.DialogResult = false;

		}

	}

}

